package com.global.variable.common.web.interceptor;

import com.global.variable.common.web.constants.Constants;
import com.global.variable.common.web.redis.RedisService;
import com.global.variable.common.web.redis.RedisServiceImpl;
import com.global.variable.common.web.utils.WebUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * company 重庆庆云石油工程技术有限责任公司
 * FileName SecurityInterceptor
 * Package com.cloud.intelligence.management.platform.intelligenceplatform.interceptor
 * Description 用户访问拦截器
 * author zjh
 * create 2018-10-18 下午 5:13
 * version V1.0
 */
@Configuration
@ConditionalOnClass(value = {HandlerInterceptor.class})
@ConditionalOnBean({RedisServiceImpl.class})
@EnableConfigurationProperties(SecurityInterceptorProperties.class)
@ConditionalOnProperty(prefix = "interceptor", value = "true", matchIfMissing = true)
public class SecurityInterceptor implements HandlerInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(SecurityInterceptor.class);


    @Autowired
    protected RedisService redisService;

    @Autowired
    private SecurityInterceptorProperties securityInterceptorProperties;




    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(!securityInterceptorProperties.getEnable()){
            return true;
        }
        logger.info("------preHandle------");
        String token = WebUtils.getCookieValue(request, Constants.TOKEN_NAME);
        if(StringUtils.isEmpty(token)){
            logger.info("------Token失效,转向登录界面------");
            response.sendRedirect("/system/to_login");
            return false;
        }

        //  每个平台端口号不一样，必须设置
        WebUtils.setCookie(response, Constants.TOKEN_NAME,token, Constants.TOKEN_EXPIRE_TIME);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        logger.info("------afterCompletion------");
        if(ex != null){
            logger.error("请求出错,出错信息:",ex);
        }
    }
}
